package com.dy.drp.controller;


import com.dy.drp.dto.PowerDto;
import com.dy.drp.dto.RolePowerDto;
import com.dy.drp.entity.Role;
import com.dy.drp.service.IRoleService;
import com.dy.drp.util.PageUtil;
import com.dy.drp.vo.RolePowerVo;
import com.dy.drp.vo.RoleVo;
import com.dy.drp.vo.UserAddVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-04-01
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理接口")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @GetMapping("/selectOnePageRole/{pageNum}/{pageSize}")
    @ApiOperation("查询一页角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value = "当前页码"),
            @ApiImplicitParam(name = "pageSize",value = "页面展示条数")
    })
    public PageUtil<Role> selectOnePageRole(@PathVariable("pageNum")Integer pageNum,
                                            @PathVariable("pageSize")Integer pageSize){
        PageUtil<Role> pageUtil = new PageUtil<>();
        pageUtil.setPageNum(pageNum);
        pageUtil.setPageSize(pageSize);
        return roleService.selectOnePageRole(pageUtil);
    }

    @PostMapping("/insertRole")
    @ApiOperation("添加角色")
    public String insertRole(RolePowerVo rolePowerVo){
        List<PowerDto> list = new ArrayList<>();
        System.out.println(rolePowerVo);
        for (int i = 0; i < rolePowerVo.getPowerList().length; i++){
            PowerDto powerDto = new PowerDto(rolePowerVo.getPowerList()[i]);
            list.add(powerDto);
        }
        RolePowerDto rolePowerDto = new RolePowerDto();
        rolePowerDto.setRoleName(rolePowerVo.getRoleName());
        rolePowerDto.setPowerDtoList(list);
        System.out.println(rolePowerDto);
        if (roleService.insertRole(rolePowerDto)){
            return "添加成功";
        }
        return "添加失败";
    }

    @GetMapping("/deleteRole/{roleName}")
    @ApiOperation("删除角色")
    @ApiImplicitParam(name="roleName",value = "角色名")
    public String deleteRole(@PathVariable("roleName")String roleName){
        if (roleService.deleteRole(roleName)){
            return "删除成功";
        }
        return "删除失败";
    }

    @PostMapping("/selectRolePowerByName")
    @ApiOperation("查看角色权限")
//    @ApiImplicitParam(name="roleName",value = "角色名称")
    public String selectRolePowerByName(RoleVo userAddVo){
        String[] roleNames = userAddVo.getRoleList();
        HashSet<String> hashSet = new HashSet<>();
        for (int i = 0; i < roleNames.length; i++){
            RolePowerDto rolePowerDto = roleService.selectRolePowerByName(roleNames[i]);
            rolePowerDto.getPowerDtoList().stream().forEach(r->{
                hashSet.add(r.getPowerName());
            });
        }
        String power = "";
        for(String s: hashSet){
            power += s + "、";
        }
        return power;
    }

    @PostMapping("/updateRolePower")
    @ApiOperation("修改角色权限")
    public String updateRolePower(RolePowerVo rolePowerVo){
        List<PowerDto> list = new ArrayList<>();
        System.out.println(rolePowerVo);
        for (int i = 0; i < rolePowerVo.getPowerList().length; i++){
            PowerDto powerDto = new PowerDto(rolePowerVo.getPowerList()[i]);
            list.add(powerDto);
        }
        RolePowerDto rolePowerDto = new RolePowerDto();
        rolePowerDto.setRoleName(rolePowerVo.getRoleName());
        rolePowerDto.setPowerDtoList(list);
        System.out.println(rolePowerDto);
        if(roleService.updateRolePower(rolePowerDto)){
            return "修改成功";
        }
        return "修改失败";
    }

    @GetMapping("/selectPowerByOneRole/{roleName}")
    @ApiOperation("查询单个角色权限")
    @ApiImplicitParam(name="roleName",value="角色名")
    public String[] selectPowerByOneRole(@PathVariable("roleName")String roleName){
        RolePowerDto rolePowerDto = roleService.selectRolePowerByName(roleName);
        List<String> powers = new ArrayList<>();
        rolePowerDto.getPowerDtoList().stream().forEach(r->{
            powers.add(r.getPowerName());
        });
        return powers.stream().toArray(String[]::new);
    }

    @GetMapping("/selectAllRoleName")
    @ApiOperation("获取所有角色")
    public List<Role> selectAllRoleName(){
        return roleService.list();
    }
}
